﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using YArchitech.Revit;

namespace HYBatchPrinter.Utils
{
	public static class ViewSheetManager
	{
		public static List<View> GetCurDocViewByType(Document doc, ViewType vType)
		{
			List<View> list = new List<View>();
			foreach (Element element in new FilteredElementCollector(doc).OfClass(typeof(View)))
			{
				View view = element as View;
				if (view != null)
				{
					list.Add(view);
				}
			}
			return list;
		}

		public static List<string> GetSheetPortsNames(ViewSheet viewsheet)
		{
			List<string> list = new List<string>();
			Document document = viewsheet.Document;
			if (document != null)
			{
				foreach (ElementId elementId in viewsheet.GetAllViewports())
				{
					Viewport viewport = document.GetElement(elementId) as Viewport;
					if (viewport != null)
					{
						ElementId viewId = viewport.ViewId;
						View view = document.GetElement(viewId) as View;
						if (view != null)
						{
							string viewTitle = RevitViewExtension.GetViewTitle(view);
							if (viewTitle.Length > 0)
							{
								list.Add(viewTitle);
							}
						}
					}
				}
			}
			return list;
		}

		public static string GetViewSheetName(ViewSheet sheet)
		{
			string result = "";
			if (sheet != null)
			{
				result = sheet.SheetNumber + "-" + sheet.ViewName;
			}
			return result;
		}

		public static List<ViewSheetSet> GetCurDocSheetSet(Document doc)
		{
			List<ViewSheetSet> list = new List<ViewSheetSet>();
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			filteredElementCollector.OfClass(typeof(ViewSheetSet));
			foreach (Element element in filteredElementCollector)
			{
				ViewSheetSet viewSheetSet = element as ViewSheetSet;
				if (viewSheetSet != null)
				{
					list.Add(viewSheetSet);
				}
			}
			return list;
		}

		public static void AddOneViewSheetToSet(ViewSheetSet set, ViewSheet sheet)
		{
			if (sheet != null && set != null)
			{
				ViewSet viewSet = new ViewSet();
				foreach (object obj in set.Views)
				{
					View view = (View)obj;
					if (view != null)
					{
						viewSet.Insert(view);
					}
				}
				viewSet.Insert(sheet);
				set.Views = viewSet;
			}
		}

		public static void DelViewSheetFromSet(ViewSheetSet set, List<ViewSheet> arSheet)
		{
			if (arSheet != null && set != null)
			{
				ViewSet viewSet = new ViewSet();
				foreach (object obj in set.Views)
				{
					View view = (View)obj;
					if (view != null)
					{
						viewSet.Insert(view);
					}
				}
				foreach (View view2 in arSheet)
				{
					if (view2 != null)
					{
						viewSet.Erase(view2);
					}
				}
				set.Views = viewSet;
			}
		}

		public static ViewSheetSet GetViewSheetSetByName(string sSetName, Document doc)
		{
			ViewSheetSet result = null;
			foreach (ViewSheetSet viewSheetSet in ViewSheetManager.GetCurDocSheetSet(doc))
			{
				if (viewSheetSet.Name.Equals(sSetName))
				{
					result = viewSheetSet;
					break;
				}
			}
			return result;
		}
	}
}
